package org.edu.modules.classSchedule.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.edu.common.api.vo.Result;
import org.edu.common.system.query.QueryGenerator;
import org.edu.modules.classChoise.entity.JwKechengClass;
import org.edu.modules.classSchedule.entity.VeJwPkmxClass;
import org.edu.modules.classSchedule.service.IVeJwPkmxClassService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.edu.common.system.base.controller.BaseController;
import org.edu.modules.jxrw.entity.VeJwJxrw;
import org.edu.modules.jxzy.entity.JwJiaoshi;
import org.edu.modules.pub.entity.VeJwJianzhu;
import org.edu.modules.pub.entity.VeJwJiaoshi;
import org.edu.modules.pub.service.IVeJwJianzhuService;
import org.edu.modules.pub.service.IVeJwJiaoshiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.edu.common.aspect.annotation.AutoLog;

/**
 * @Description: 排课明细
 * @Author:
 * @Date: 2021-05-13
 * @Version: V1.0
 */
@Api(tags = "排课信息明细")
@RestController
@RequestMapping("/classSchedule/veJwPkmx")
@Slf4j
public class VeJwPkmxClassController extends BaseController<VeJwPkmxClass, IVeJwPkmxClassService> {
    @Autowired
    private IVeJwPkmxClassService veJwPkmxService;

    @Autowired
    private IVeJwJiaoshiService veJwJiaoshiService;

    @Autowired
    private IVeJwJianzhuService veJwJianzhuService;

    /**
     * 分页查询
     *
     * @param veJwPkmx
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "排课明细-分页查询")
    @ApiOperation(value = "明细查询", notes = "排课明细-分页查询")
    @PostMapping(value = "/list")
    public Result<?> queryPageList(VeJwPkmxClass veJwPkmx,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "99") Integer pageSize,
                                   HttpServletRequest req) {
        /*QueryWrapper<VeJwPkmxClass> queryWrapper = QueryGenerator.initQueryWrapper(veJwPkmx, req.getParameterMap());
        Page<VeJwPkmxClass> page = new Page<VeJwPkmxClass>(pageNo, pageSize);
        IPage<VeJwPkmxClass> pageList = veJwPkmxService.page(page, queryWrapper);*/

        QueryWrapper<VeJwPkmxClass> queryWrapper = QueryGenerator.initQueryWrapper(veJwPkmx, req.getParameterMap());
        Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageNo, pageSize);
        IPage<Map<String, Object>> pageList = veJwPkmxService.pageMaps(page, queryWrapper);
        List<Map<String, Object>> VeJwPkmxClasss = pageList.getRecords();
        for (Map<String, Object> map : VeJwPkmxClasss) {
            if (map.get("jsid") != null) {
                Integer jsid = Integer.parseInt(map.get("jsid").toString());
                VeJwJiaoshi jwJiaoshi = veJwJiaoshiService.getById(jsid);
                if (jwJiaoshi != null) {
                    map.put("jsmc", jwJiaoshi.getJsmc());
                    try {
                        VeJwJianzhu veJwJianzhu = veJwJianzhuService.getById(jwJiaoshi.getJzid());
                        if (jwJiaoshi != null) {
                            map.put("jzid", veJwJianzhu.getId());
                            map.put("jzwmc", veJwJianzhu.getJzwmc());
                        } else {
                            map.put("jzwmc", null);
                        }
                    } catch (Exception e) {
                        map.put("jzwmc", null);
                    }

                }
            } else {
                map.put("jsmc", null);
            }
        }
        pageList.setRecords(VeJwPkmxClasss);
        return Result.OK(pageList);
    }

    /**
     * 学生课表查询
     *
     * @param req
     * @return
     */
    @AutoLog(value = "排课明细-学生课表查询")
    @ApiOperation(value = "学生课表查询", notes = "排课明细-学生课表查询")
    @PostMapping(value = "/studentSchedule")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stuid", value = "学生id", required = true),
            @ApiImplicitParam(name = "semid", value = "学期id", required = true)
    })
    public Result<?> studentSchedule(Integer stuid,
                                     Integer semid,
                                     HttpServletRequest req) {
        VeJwPkmxClass veJwPkmx = new VeJwPkmxClass();
        QueryWrapper<VeJwPkmxClass> queryWrapper = QueryGenerator.initQueryWrapper(veJwPkmx, req.getParameterMap());
        if (stuid != null && !stuid.equals("")) {
            queryWrapper.eq("x.stuid", stuid);
        }
        if (semid != null && !semid.equals("")) {
            queryWrapper.eq("p.semid", semid);
            queryWrapper.eq("x.semid", semid);
        }
        List<Map<String, Object>> list = veJwPkmxService.studentSchedule(queryWrapper);
        return Result.OK(list);
    }

    /**
     * 教师课表查询
     *
     * @param req
     * @return
     */
    @AutoLog(value = "排课明细-教师课表查询")
    @ApiOperation(value = "教师课表查询", notes = "排课明细-教师课表查询")
    @PostMapping(value = "/teacherSchedule")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teacherid", value = "教师id", required = true),
            @ApiImplicitParam(name = "semid", value = "学期id", required = true)
    })
    public Result<?> teacherSchedule(Integer teacherid,
                                     Integer semid,
                                     HttpServletRequest req) {
        VeJwPkmxClass veJwPkmx = new VeJwPkmxClass();
        QueryWrapper<VeJwPkmxClass> queryWrapper = QueryGenerator.initQueryWrapper(veJwPkmx, req.getParameterMap());
        if (teacherid != null && !teacherid.equals("")) {
            queryWrapper.eq("m.teacherid", teacherid);
        }
        if (semid != null && !semid.equals("")) {
            queryWrapper.eq("m.semid", semid);
        }

        List<Map<String, Object>> list = veJwPkmxService.teacherSchedule(queryWrapper);
        return Result.OK(list);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "排课明细-通过id查询")
    @ApiOperation(value = "通过id查询", notes = "排课明细-通过id查询")
    @PostMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        VeJwPkmxClass veJwPkmx = veJwPkmxService.getById(id);
        if (veJwPkmx == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(veJwPkmx);
    }

    /**
     * 新增信息
     *
     * @param veJwPkmx
     * @return
     */
    @AutoLog(value = "排课明细-新增信息")
    @ApiOperation(value = "新增信息", notes = "排课明细-新增信息")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeJwPkmxClass veJwPkmx) {
        LambdaQueryWrapper<VeJwPkmxClass> queryWrapper = new LambdaQueryWrapper<>();
        //判断这个时间这个教室是否有排课
        queryWrapper.eq(VeJwPkmxClass::getSemid, veJwPkmx.getSemid());
        queryWrapper.eq(VeJwPkmxClass::getWeek, veJwPkmx.getWeek());
        queryWrapper.eq(VeJwPkmxClass::getSection, veJwPkmx.getSection());
        queryWrapper.eq(VeJwPkmxClass::getJsid, veJwPkmx.getJsid());
        VeJwPkmxClass veJwPkmxClass = veJwPkmxService.getOne(queryWrapper);
        if (veJwPkmxClass == null) {
            //如果这个时间这个教室没排课，再判断这个时间这个教师是否有排课
            //判断这个时间这个教师是否有排课
            LambdaQueryWrapper<VeJwPkmxClass> queryWrapper2 = new LambdaQueryWrapper<>();
            //判断这个时间这个教师是否有排课
            queryWrapper2.eq(VeJwPkmxClass::getSemid, veJwPkmx.getSemid());
            queryWrapper2.eq(VeJwPkmxClass::getWeek, veJwPkmx.getWeek());
            queryWrapper2.eq(VeJwPkmxClass::getSection, veJwPkmx.getSection());
            queryWrapper2.eq(VeJwPkmxClass::getTeacherid, veJwPkmx.getTeacherid());
            VeJwPkmxClass veJwPkmxClass2 = veJwPkmxService.getOne(queryWrapper2);
            if (veJwPkmxClass2 == null) {
                if (veJwPkmxService.save(veJwPkmx)) {
                    return Result.OK("添加成功！");
                }
            } else {
                return Result.error("当前时间当前教师有排课，不能排课");
            }
        } else {
            return Result.error("当前时间当前教室有排课，不能排课");
        }
        //判断这个时间这个教师是否有排课
        /*LambdaQueryWrapper<VeJwPkmxClass> queryWrapper2 = new LambdaQueryWrapper<>();
        //判断这个时间这个教师是否有排课
        queryWrapper2.eq(VeJwPkmxClass::getSemid, veJwPkmx.getSemid());
        queryWrapper2.eq(VeJwPkmxClass::getWeek, veJwPkmx.getWeek());
        queryWrapper2.eq(VeJwPkmxClass::getSection, veJwPkmx.getSection());
        queryWrapper2.eq(VeJwPkmxClass::getTeacherid, veJwPkmx.getTeacherid());
        VeJwPkmxClass veJwPkmxClass2 = veJwPkmxService.getOne(queryWrapper2);
        if (veJwPkmxClass2 == null) {
            if (veJwPkmxService.save(veJwPkmx)) {
                return Result.OK("添加成功！");
            }
        } else {
            return Result.error("当前时间当前教师有排课，不能排课");
        }*/
        return Result.error("添加失败!");
    }

    /**
     * 修改信息
     *
     * @param veJwPkmx
     * @return
     */
    @AutoLog(value = "排课明细-修改信息")
    @ApiOperation(value = "修改信息", notes = "排课明细-修改信息")
    @PostMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeJwPkmxClass veJwPkmx) {
        if (veJwPkmxService.updateById(veJwPkmx)) {
            return Result.OK("编辑成功!");
        }
        return Result.error("编辑失败!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "排课明细-通过id删除")
    @ApiOperation(value = "通过id删除", notes = "排课明细-通过id删除")
    @PostMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        if (veJwPkmxService.removeById(id)) {
            return Result.OK("删除成功!");
        }
        return Result.error("删除失败!");
    }

    /**
     * 批量删除
     *
     * @param pkId
     * @return
     */
    @AutoLog(value = "排课明细-批量删除")
    @ApiOperation(value = "清空排课明细", notes = "排课明细-批量删除")
    @PostMapping(value = "/deleteByPkId")
    public Result<?> deleteByPkId(@RequestParam(name = "pkId", required = true) String pkId) {
        if (veJwPkmxService.deleteByPkId(pkId)) {
            return Result.OK("清空排课明细成功!");
        }
        return Result.error("清空排课明细失败!");
    }

    /**
     * 周课表查询
     *
     * @return
     */
    @AutoLog(value = "排课明细-周课表查询")
    @ApiOperation(value = "周课表查询", notes = "排课明细-周课表查询")
    @PostMapping(value = "/weekSchedule")
    public Result<?> weekSchedule1(@RequestParam(value = "bj_id", required = false) Integer bjId, @RequestParam(value = "teacherId", required = false) Integer teacherId,
                                   Integer semId, Integer courseId,
                                   HttpServletRequest req) {
        VeJwPkmxClass veJwPkmx = new VeJwPkmxClass();
        QueryWrapper<VeJwPkmxClass> queryWrapper = QueryGenerator.initQueryWrapper(veJwPkmx, req.getParameterMap());
        if (teacherId != null && !teacherId.equals("")) {
            queryWrapper.eq("m.teacherid", teacherId);
        }
        if (semId != null && !semId.equals("")) {
            queryWrapper.eq("m.semid", semId);
        }
        if (bjId != null && !bjId.equals("")) {
            queryWrapper.eq("p.jxbid", bjId);
        }
        if (courseId != null && !courseId.equals("")) {
            queryWrapper.eq("m.kcid", courseId);
        }
        List<Map<String, Object>> list = veJwPkmxService.weekSchedule(queryWrapper);
        return Result.OK(list);
    }

    @AutoLog(value = "排课-老师学期查询")
    @ApiOperation(value = "老师学期查询", notes = "排课-老师学期查询")
    @GetMapping(value = "/teacherSmeIds")
    @ApiImplicitParams({@ApiImplicitParam(name = "teacherid", value = "教师id", required = true)
    })
    public Result<?> getTeacherSmeIds(String teacherid) {
        List<Map<String, Object>> list = veJwPkmxService.teacherSemId(teacherid);
        return Result.OK(list);
    }

    @AutoLog(value = "排课-老师课表查询")
    @ApiOperation(value = "老师课表查询", notes = "排课-老师课表查询")
    @GetMapping(value = "/teacherkcb")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teacherid", value = "教师id", required = true),
            @ApiImplicitParam(name = "bjid", value = "班级Id", required = true)
    })
    public Result<?> getTeacherkcb(String bjid, String teacherid) {
        List<Map<String, Object>> list = veJwPkmxService.teacherkcb(bjid, teacherid);
        return Result.OK(list);
    }
}
